// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
import RecycleList from "./RecycleList";
const { ccclass, property } = cc._decorator;
export enum VerticalLayout {
    TOP_TO_BOTTOM,
    BOTTOM_TO_TOP
}

export enum HorizontalLayout {
    LEFT_TO_RIGHT,
    RIGHT_TO_LEFT
}

//临时变量
let n1: cc.Node;
let n2: cc.Node;
let v1 = cc.Vec2.ZERO;
let v2 = cc.Vec2.ZERO;
let v3 = cc.Vec2.ZERO;

@ccclass
export default abstract class RecycleListLayout extends cc.Component {

    @property({
        type: cc.Enum(VerticalLayout)
    })
    verticalLayout: VerticalLayout = VerticalLayout.TOP_TO_BOTTOM;

    @property({
        type: cc.Enum(HorizontalLayout)
    })
    horizontalLayout: HorizontalLayout = HorizontalLayout.LEFT_TO_RIGHT;

    public recycleList: RecycleList = null;

    public abstract resetLayout();
    public abstract updateLayout();
    public abstract onContentPositionChanged();
    public abstract getItemPositionByDataIndex(dataIndex: number): cc.Vec2;

    public abstract getScrollOffset(dataIndex: number, percentX: number, percentY: number): cc.Vec2;
    public abstract getItemLength(): number;
    public abstract isDataIndexInView(dataIndex: number): boolean;
    public abstract dataIndex2ItemIndex(dataIndex: number): number;
    public abstract itemIndex2DataIndex(itemIndex: number): number;


    protected getContentOffset(): cc.Vec2 {
        n1 = this.recycleList.view, n2 = this.recycleList.content;
        //view左下角世界坐标
        v1.x = -n1.width, v1.y = -n1.height;
        v2.x = n1.anchorX, v2.y = n1.anchorY;
        n1.convertToWorldSpaceAR(v1.multiply(v2), v1);
        //content左下角世界坐标
        v3.x = -n2.width, v3.y = -n2.height;
        v2.x = n2.anchorX, v2.y = n2.anchorY;
        n2.convertToWorldSpaceAR(v3.multiply(v2), v3);

        let offset = cc.Vec2.ZERO;
        offset.y = this.verticalLayout === VerticalLayout.TOP_TO_BOTTOM ? v3.y + n2.height - n1.height - v1.y : v1.y - v3.y;
        offset.x = this.horizontalLayout === HorizontalLayout.LEFT_TO_RIGHT ? v1.x - v3.x : v3.x + n2.width - n1.width - v1.x;
        return offset;
    }

    /**
     * 基于左下角设置content坐标
     * @param position 
     */
    public setContentPosition(position: cc.Vec2) {
        let view = this.recycleList.view, content = this.recycleList.content;
        let pos = cc.Vec2.ZERO;
        pos.y = this.verticalLayout === VerticalLayout.TOP_TO_BOTTOM ? -content.height + view.height : 0;
        pos.x = this.horizontalLayout === HorizontalLayout.LEFT_TO_RIGHT ? 0 : -content.width + view.width;
        pos.addSelf(content.getAnchorPoint().multiply(cc.v2(content.width, content.height)));
        pos.subSelf(view.getAnchorPoint().multiply(cc.v2(view.width, view.height)));
        pos.equals(content.getPosition()) ? this.onContentPositionChanged() : content.setPosition(pos.addSelf(position));
    }
}